package com.north.light.androidutils.canvas.selectionprogressbar;

import android.animation.Animator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.airbnb.lottie.LottieAnimationView;
import com.north.light.androidutils.log.LogUtil;

/**
 * FileName: SelectionLottieView
 * Author: lzt
 * Date: 2022/10/9 10:47
 * 选项动画view
 */
public class SelectionLottieView extends RelativeLayout {
    private LottieAnimationView mAnimView;

    public SelectionLottieView(Context context) {
        super(context);
        init();
    }

    public SelectionLottieView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SelectionLottieView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onDetachedFromWindow() {
        cancel();
        super.onDetachedFromWindow();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    private void init() {
        LogUtil.d("lottie view init");
        mAnimView = new LottieAnimationView(getContext());
        addView(mAnimView);
        RelativeLayout.LayoutParams animRootParams = (LayoutParams) mAnimView.getLayoutParams();
        animRootParams.width = LayoutParams.MATCH_PARENT;
        animRootParams.height = LayoutParams.MATCH_PARENT;
        mAnimView.setLayoutParams(animRootParams);


        mAnimView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        mAnimView.removeAllAnimatorListeners();
        mAnimView.addAnimatorListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                LogUtil.d("onAnimationStart");
                setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                LogUtil.d("onAnimationEnd");
                setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                LogUtil.d("onAnimationCancel");
                setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                LogUtil.d("onAnimationRepeat");
                setVisibility(View.VISIBLE);
            }
        });
        setVisibility(View.INVISIBLE);
    }

    //外部调用方法------------------------------------------------------------------------------


    /**
     * mAnimView.setImageAssetsFolder("lp");
     * mAnimView.setAnimation("lp/refresh.json");
     */
    public void start(String resPath, String resName) {
        LogUtil.d("start");
        if (mAnimView != null) {
            pause();
            setVisibility(View.VISIBLE);
            mAnimView.setImageAssetsFolder(resPath);
            mAnimView.setAnimation(resName);
            mAnimView.setRepeatCount(0);
            mAnimView.playAnimation();
        }
    }

    public void pause() {
        if (mAnimView != null) {
            mAnimView.pauseAnimation();
        }
    }

    public void cancel() {
        if (mAnimView != null) {
            mAnimView.cancelAnimation();
        }
    }

    public boolean isPlaying() {
        if (mAnimView == null) {
            return false;
        }
        return mAnimView.isAnimating();
    }


}
